using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using Zsa.Emulation.Cartridge;
using Zsa.Emulation.Render;
using Zsa.Emulation.Util;

namespace Zsa.Emulation.CNes
{
    public static class Program
    {

        
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                while (Run()) { }

            }
            catch (Exception excep)
            {
                TraceLogger.TraceInformation("Exception in main !!");
                TraceLogger.TraceInformation(excep.ToString());
                TraceLogger.FlushAll();

                #if !DEBUG
                MessageBox.Show("Unexpected error.  Must exist !");
                #endif
            }
            finally
            {
                TraceLogger.FlushAll();
            }
        }

        /// <summary>
        /// Runs this instance.
        /// </summary>
        /// <returns></returns>
        static bool Run()
        {
            bool _Restart = false;
            try
            {
                using (NesMachineManager<MainForm> _DeviceHost = new NesMachineManager<MainForm>())
                {
                    _DeviceHost.Start();
                    _DeviceHost.Run();
                }
            }
            catch ( Exception excep )
            {
                TraceLogger.TraceInformation("Exception in main !!");
                TraceLogger.TraceInformation(excep.ToString());

                _Restart = MessageBox.Show("Unexpected error occured !  Restart?\r\n\r\n" +
                    excep.ToString(), "ERROR", MessageBoxButtons.YesNo) == DialogResult.Yes;
            }
            return _Restart;
        }

    }
}